/**
 * @author 黄波
 * 创建日期：2011Oct 2, 2011
 * TODO// N/A
 */
package com.platform.course.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.platform.common.bo.CommonBo;
import com.platform.course.bo.CourseBo;
import com.platform.pojo.CourseDirectory;
import com.platform.pojo.CourseInfo;
import com.platform.pojo.ResInfo;
import com.platform.pojo.SchoolProperty;
import com.platform.pojo.UserInfo;
import com.platform.res.bo.ResBo;
import com.platform.user.bo.UserBo;

/**
 * 查询课程目录
 */
public class LookupCourseDirAction extends CourseSupport {

	private String courseId;	//课程编号
	private String courseName;	//课程名称
	private UserInfo teacher;	//任课老师
	private String forClass;	//授课班级
	private String classId;	//授课班级ID
	private List<CourseDirectory> dirs;	//课程目录列表
	private String superId;	//目录父ID
	private CourseDirectory currentDir;	//当前目录实体信息
	
	private List<ResInfo> reslist;	//查询对应该课程下的资源
	
	public List<ResInfo> getReslist() {
		return reslist;
	}

	public void setReslist(List<ResInfo> reslist) {
		this.reslist = reslist;
	}

	public List<CourseDirectory> getDirs() {
		return dirs;
	}

	public void setDirs(List<CourseDirectory> dirs) {
		this.dirs = dirs;
	}

	public String getSuperId() {
		return superId;
	}

	public void setSuperId(String superId) {
		this.superId = superId;
	}

	public String getCourseId() {
		return courseId;
	}

	public void setCourseId(String courseId) {
		this.courseId = courseId;
	}

	@Override
	public String execute() throws Exception {
		if(courseId == null || courseId.equals("")) {
			return ERROR;
		}
		CourseBo bo = CourseBo.getInstance();
		if(superId == null || superId.equals("")) {
			superId = "0";
		}
		dirs = bo.getCourseDirectories(courseId, superId);

		currentDir = bo.getCourseDirectory(superId);
		if(currentDir == null) {
			//防止根目录情况
			currentDir = new CourseDirectory();
			currentDir.setId(0);
			currentDir.setFolderEtc(1);
			currentDir.setFolderName("root");
			currentDir.setCourseId(Integer.parseInt(courseId));
		}
		//查询任课老师
		CourseInfo item = bo.getCourse(courseId);
		//课程名称
		if(courseName == null || courseName.equals("")) {
			courseName = item.getCourseName();
		}
		if(teacher == null) {
			//查询课程实体信息
			UserBo userBo = UserBo.getInstance();
			teacher = userBo.getUser(item.getUserId().toString());
		}
		//授课班级
		if(forClass == null || forClass.equals("")) {
			CommonBo commonBo = CommonBo.getInstance();
			SchoolProperty sp = commonBo.getSchoolProperty(item.getClassId().toString());
			if(sp.getIsClass() == 1) {
				forClass = sp.getName();
				classId = sp.getId().toString();
			}
		}

		HttpSession session = ServletActionContext.getRequest().getSession();
		final UserInfo user = (UserInfo) session.getAttribute("user");
		if(user == null) {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out.flush();
			out.write("<script>alert('请先登录！');history.back();</script>");
			out.close();
			return null;
		}
		//查看该学生是否是属性该班级
		if(user.getId() != teacher.getId() && user.getClassId() != new Integer(classId)) {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out.flush();
			out.write("<script>alert('非该班级人员无权查看！');history.back();</script>");
			out.close();
			return null;
		}
		if(user.getIsAudit() == 0) {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out.flush();
			out.write("<script>alert('帐户尚未解锁，无权查看！');history.back();</script>");
			out.close();
			return null;
		}
		//查询该文件夹下对应的资源
		ResBo resBo = ResBo.getInstance();
		reslist = resBo.getResInfoByFolderId(superId);
		return SUCCESS;
	}

	public String getCourseName() {
		return courseName;
	}

	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}


	public UserInfo getTeacher() {
		return teacher;
	}

	public void setTeacher(UserInfo teacher) {
		this.teacher = teacher;
	}

	public String getForClass() {
		return forClass;
	}

	public void setForClass(String forClass) {
		this.forClass = forClass;
	}

	public String getClassId() {
		return classId;
	}

	public void setClassId(String classId) {
		this.classId = classId;
	}

	public CourseDirectory getCurrentDir() {
		return currentDir;
	}

	public void setCurrentDir(CourseDirectory currentDir) {
		this.currentDir = currentDir;
	}

	
}
